package com.borzaindustries.mkdummy;

import android.app.*;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.*;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.*;
import android.view.View.*;

public class MkDummyActivity extends Activity {

	private static final int DIALOG_EXIT = 0;
	private static final int DIALOG_HELP = 1;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView comp = (TextView) findViewById(R.id.company_logo);
		comp.setMovementMethod(LinkMovementMethod.getInstance());
		comp.setText(Html.fromHtml(this.getString(R.string.company)));
		comp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// open browser with company site
				Intent intent = new Intent("android.intent.action.VIEW",
						Uri.parse(getBaseContext().getString(
								R.string.company_url)));
				startActivity(intent);
			}
		});
		((Button) findViewById(R.id.button_create_file))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getBaseContext(),
								CreateBinaryDummy.class);
						startActivity(intent);
					}
				});
		((Button) findViewById(R.id.button_create_text))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(getBaseContext(),
								CreateTextDummy.class);
						startActivity(intent);
					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == DirectoryBrowser.BROWSE_DIR) {
			if (resultCode != RESULT_OK)
				return;
			Toast.makeText(
					getBaseContext(),
					"selected directory "
							+ data.getStringExtra(DirectoryBrowser.SELECTED_PATH),
					Toast.LENGTH_SHORT).show();
		}
	}

	// OPTIONS MENU
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_quit:
			showDialog(DIALOG_EXIT);
			return true;
		case R.id.menu_help:
			showDialog(DIALOG_HELP);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_EXIT:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure you want to exit?")
					.setCancelable(true)
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
								}
							});
			return builder.create();
		case DIALOG_HELP:
			Builder b = new Builder(this);
			// TODO show on another screen??
			b.setMessage("help");
			return b.create();
		default:
			return null;
		}
	}
}